package com.boarsoft.boar.gateway.weixin.common.http;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLSocket;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

public class WXSSLSocketFactory implements SecureProtocolSocketFactory{

	public WXSSLSocketFactory(){
		
	}
	
	public Socket createSocket(Socket socket, String host, int port, boolean arg3)
			throws IOException, UnknownHostException {
		return WeixinX509TrustManager.getSSLSocketFactory().createSocket(socket, host, port, arg3);
	}

	public Socket createSocket(String host, int port) throws IOException,
			UnknownHostException {
		return WeixinX509TrustManager.getSSLSocketFactory().createSocket(host, port);
	}

	public Socket createSocket(String host, int port, InetAddress arg2, int arg3)
			throws IOException, UnknownHostException {
		SSLSocket socket = (SSLSocket)WeixinX509TrustManager.getSSLSocketFactory().createSocket(host, port);
		return socket;
	}

	@Override
	public Socket createSocket(String host, int port, InetAddress address,
			int localPort, HttpConnectionParams params) throws IOException,
			UnknownHostException, ConnectTimeoutException {
		if (params == null)
		      throw new IllegalArgumentException("Parameters can not be null");
		    int timeout = params.getConnectionTimeout();
		    if (timeout == 0) {
		      return createSocket(host, port, address, localPort);
		    }
		    return ControllerThreadSocketFactory.createSocket(this, host, port, address, localPort, timeout);
	}

}
